<?php

class createController extends controllerBase {
	protected $mode = AETHER_CLI;
	protected $view = false;
	
	function controller() {
		echo 'Creating controller... ';
		$name = $this->getParam('id');
		$args = $this->getParam('args');
		if(!$fh = fopen('app/controllers/'.$name.'Controller.php','x')) {
			echo 'Failed.  Could not create file.';
			return;
		}
		if(!file_exists('app/views/'.$name)) {
			if(!mkdir('app/views/'.$name)) {
				echo 'Could not create directory for views';
				return;
			}
		}
		$fc = 
		'<?php'.NL.
		NL.
		'class '.$name.'Controller extends controllerBase {'.NL.
		TAB.'protected $mode = AETHER_BOTH;'.NL.
		TAB.'protected $view = true;'.NL.
		TAB.'protected $helpers = array();'.NL.
		TAB.'protected $models = array();'.NL.
		TAB.'protected $beforeFilters = array();'.NL.
		TAB.'protected $beforeAfter = array();'.NL;
		foreach($args['a'] as $action) {
			$fc .= TAB.NL.
			TAB.'function '.$action.'() {'.NL.
			TAB.TAB.'$this->setVar(\'title\', \'Untitled\');'.NL.
			TAB.'}'.NL;
			if(!$fh2 = fopen('app/views/'.$name.'/'.$action.'.php','x')) {
				echo 'Could not create file for the '.$action.' view.';
				return;
			}
			$fc2 =
			'<h1>You haven\'t created this view yet!</h1>'.NL.
			'<p>Open up &quot;app/views/'.$name.'/'.$action.'.php&quot; to get started.</p>'.NL;
			fwrite($fh2,$fc2);
			fclose($fh2);
		}
		$fc .= '}'.NL.
		NL.
		'?>'.NL;
		fwrite($fh,$fc);
		fclose($fh);
		echo 'Done.';
	}
	
	function helper() {
		echo 'Creating helper... ';
		$name = $this->getParam('id');
		$args = $this->getParam('args');
		if(!$fh = fopen('app/helpers/'.$name.'Helper.php','x')) {
			echo 'Failed.  Could not create file.';
			return;
		}
		$fc = 
		'<?php'.NL.
		NL.
		'class '.$name.'Helper extends helperBase {'.NL.
		TAB.NL;
		if(isset($args['f'])) {
			foreach($args['f'] as $function) {
				$fc .=
				TAB.'function '.$function.'() {'.NL.
				TAB.TAB.NL.
				TAB.'}'.NL.
				TAB.NL;
			}
		}
		$fc .=
		'}'.NL.
		NL.
		'?>'.NL;
		fwrite($fh,$fc);
		fclose($fh);
		echo 'Done.';
	}
	
	function model() {
		echo 'Creating model... ';
		$name = $this->getParam('id');
		$args = $this->getParam('args');
		if(!$fh = fopen('app/models/'.$name.'Model.php','x')) {
			echo 'Failed.  Could not create file.';
			return;
		}
		$fc = 
		'<?php'.NL.
		NL.
		'class '.$name.'Model extends modelBase {'.NL.
		TAB.NL.
		TAB.'protected $name = \''.$name.'\';'.NL.
		TAB.'protected $dbLink = true;'.NL.
		TAB.'protected $cols = array('.NL.
		TAB.TAB;
		if(isset($args['c'])) {
			$first = true;
			foreach($args['c'] as $col) {
				if($first)
					$first = false;
				else
					$fc .= ','.NL.TAB.TAB;
				$fc .= '\''.$col.'\' => array(';
				if($col=='id'||$col=='updatedAt'||$col=='createdAt')
					$fc .= '\'auto\' => true';
				$fc .= ')';
			}
		}
		$fc .=
		NL.TAB.');'.NL.
		TAB.'protected $validations = array('.NL.
		TAB.TAB.NL.
		TAB.');'.NL.
		'}'.NL.
		NL.
		'?>'.NL;
		fwrite($fh,$fc);
		fclose($fh);
		echo 'Done.';
	}
}

?>
